﻿using System.Windows.Controls;
using System.Windows.Input;
using SilverlightApplication.UIControls;
using LinqToAmazonSL;
using SilverlightApplication.LanguageResources;

namespace SilverlightApplication
{
    public partial class DVDAdvancedPanel : UserControl, ISearchPanelContent
	{
        SearchPanels parent;

        public DVDAdvancedPanel(SearchPanels parent)
		{
			// Required to initialize variables
			InitializeComponent();

            asDVDTitle.Text = LanguageLoader.GetString("asTitle");
            asDVDActor.Text = LanguageLoader.GetString("asActor");
            asDVDDirector.Text = LanguageLoader.GetString("asDirector");
            asDVDKeywords.Text = LanguageLoader.GetString("asKeywords");
            asDVDEditor.Text = LanguageLoader.GetString("asEditor");

            asDVDOrderBy.Text = LanguageLoader.GetString("asOrderBy");
            asDVDPrice.Text = LanguageLoader.GetString("asPrice");
            rbDVDUpward.Content = LanguageLoader.GetString("rbDvdUpward");
            rbDVDDownward.Content = LanguageLoader.GetString("rbDvdDownward");

            emptycmbdvd.Content = LanguageLoader.GetString("emptycmbdvd");
            emptycmbdvd1.Content = LanguageLoader.GetString("emptycmbdvd");
            cbItDVDPrice.Content = LanguageLoader.GetString("cbItDVDPrice");
            cbItDVDTitle.Content = LanguageLoader.GetString("cbItDVDTitle");

            cboxDVDItemFrom10to50.Content = LanguageLoader.GetString("cboxItemFrom10to50");
            cboxDVDItemMorethan50.Content = LanguageLoader.GetString("cboxItemMorethan50");
            cboxDVDItemUp10.Content = LanguageLoader.GetString("cboxItemUp10");

            this.parent = parent;
		}

        public SearchCriteria GetSearchCriteria()
        {
            SearchCriteria criteria = new SearchCriteria();
            AmazonDVD dvd = new AmazonDVD();

            // Begin Advanced DVD Search
            dvd.Title = txbAdvDVDTitle.Text.Trim();
            dvd.Actor = txbAdvDVDActor.Text.Trim();
            dvd.Director = txbAdvDVDDirector.Text.Trim();
            dvd.Publisher = txbAdvDVDEditor.Text.Trim();

            string orderBy = ((ComboBoxItem)cboxAdvDVDOrderBy.SelectedItem).Content.ToString();

            if (orderBy == LanguageLoader.GetString("cbItDVDPrice"))
            {
                criteria.OrderBy = OrderClass.Price;
            }
            else if (orderBy == LanguageLoader.GetString("cbItDVDTitle"))
            {
                criteria.OrderBy = OrderClass.Title;
            }
            else
            {
                criteria.OrderBy = OrderClass.None;
            }

            criteria.Direction = (bool)rbDVDUpward.IsChecked ? OrderDirection.Ascending : OrderDirection.Descending;

            dvd.Keywords = txbAdvDVDKeywords.Text.Trim();

            dvd.MaximumPrice = "";
            dvd.MinimumPrice = "";

            if (((ComboBoxItem)cboxAdvDVDPrice.SelectedItem) != null)
            {
                string price = ((ComboBoxItem)cboxAdvDVDPrice.SelectedItem).Content.ToString();
                if (price == LanguageLoader.GetString("cboxItemUp10"))
                {
                    dvd.MaximumPrice = "1000";
                }
                else if (price == LanguageLoader.GetString("cboxItemFrom10to50"))
                {
                    dvd.MaximumPrice = "5000";
                    dvd.MinimumPrice = "1000";
                }
                else if (price == LanguageLoader.GetString("cboxItemMorethan50"))
                {
                    dvd.MinimumPrice = "5000";
                }
            }

            criteria.Item = dvd;

            return criteria;
        }

        public bool FieldsValidate()
        {
            return !txbAdvDVDTitle.Text.Equals(string.Empty) || !txbAdvDVDActor.Text.Equals(string.Empty)
                    || !txbAdvDVDDirector.Text.Equals(string.Empty) || !txbAdvDVDKeywords.Text.Equals(string.Empty)
                    || !txbAdvDVDEditor.Text.Equals(string.Empty)
                    || ((ComboBoxItem)cboxAdvDVDOrderBy.SelectedItem == cbItDVDTitle && !txbAdvDVDTitle.Text.Equals(string.Empty))
                    || ((ComboBoxItem)cboxAdvDVDOrderBy.SelectedItem == cbItDVDPrice && (ComboBoxItem)cboxAdvDVDPrice.SelectedItem != emptycmbdvd1)
                    || (ComboBoxItem)cboxAdvDVDOrderBy.SelectedItem != emptycmbdvd
                    || (ComboBoxItem)cboxAdvDVDPrice.SelectedItem != emptycmbdvd1;
        }

        public void ClearFields()
        {
            txbAdvDVDActor.Text = "";
            txbAdvDVDDirector.Text = "";
            txbAdvDVDTitle.Text = "";
            txbAdvDVDKeywords.Text = "";
            txbAdvDVDEditor.Text = "";
            cboxAdvDVDOrderBy.SelectedItem = emptycmbdvd;
            cboxAdvDVDPrice.SelectedItem = emptycmbdvd1;
        }

        private void KeyPress(object sender, KeyEventArgs e)
        {
            if (Key.Enter.Equals(e.Key))
            {
                parent.beginSearch();
            }
        }

        private void cboxAdvDVDOrderBy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboxAdvDVDOrderBy != null)
            {
                if (((ComboBoxItem)cboxAdvDVDOrderBy.SelectedItem) == cbItDVDTitle)
                {
                    rbDVDDownward.IsEnabled = false;
                    rbDVDUpward.IsChecked = true;
                }
                else
                {
                    rbDVDDownward.IsEnabled = true;
                }
            }
        }
	}
}